﻿Shader "Seres/ReflectPlaneShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [Normal]_NormalTex("NormalTex",2D)="bump"{}
        _ReflectNoise("ReflectNoise",Range(0,100))=20
        [Header(NORMAL1)]
        _Normal1Str("NormalScale1",Range(0,1))=1
        _scale1("Scale1",float)=1
        _flowSpeed1("FlowSpeed1",Range(-1,1))=0.5
        [Header(NORMAL2)]
        _Normal2Str("NormalScale2",Range(0,1))=1
        _scale2("Scale2",float)=-1
        _flowSpeed2("FlowSpeed2",Range(-1,1))=0.5
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" }
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        //ZWrite Off
        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float4 texcoordNor:TEXCOORD3;
            };

            TEXTURE2D(_ReflectColorRT);
            sampler LinearClampSampler;
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_NormalTex);
            SAMPLER(sampler_NormalTex);
            float _flowSpeed1;
            float _flowSpeed2;
            float _scale1;
            float _scale2;
            float _Normal1Str;
            float _Normal2Str;
            float _ReflectNoise;
            float4 _NormalTex_TexelSize;
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = TransformObjectToHClip(v.vertex);
                //o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv = v.uv;
                o.texcoordNor.xy= o.uv * _scale1 +_Time.y * _flowSpeed1*0.05;
                o.texcoordNor.zw= o.uv * _scale2 +_Time.y * _flowSpeed2*0.05;
                o.uv =  ComputeScreenPos(o.vertex);

                return o;
            }

            
            float3  NormalBlend(float3 A, float3 B)//从shadergraph里白嫖的算法
            {
                return normalize(float3(A.rg + B.rg, A.b * B.b));
            }

            float4 frag (v2f i) : SV_Target
            {
                float4 texcolor=SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv);
                float4 normaltex1=SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.texcoordNor.xy);
                float3 TSnormal1=UnpackNormalScale(normaltex1,_Normal1Str);

                float4 normaltex2=SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.texcoordNor.zw);
                float3 TSnormal2=UnpackNormalScale(normaltex2,_Normal2Str);
                float3 normalMix=NormalBlend(TSnormal2,TSnormal1);

                float2 UVoffset= normalMix.xy * _NormalTex_TexelSize.xy * _ReflectNoise;

                // sample the texture
                half2 screenUV = i.uv.xy/i.uv.w;
                float4 col = SAMPLE_TEXTURE2D(_ReflectColorRT, LinearClampSampler, screenUV+UVoffset);
                clip(col.a-0.5);

                return float4(col.rgba);
            }
            ENDHLSL
        }
    }
}
